#add O14 Search Crawl Account


function addFullRead([String] $commonname, [String] $username, [String] $url){

$permissions = "FullRead"

$uri = New-Object System.Uri($url)
$webApp = [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup($uri)

$policy = $webApp.Policies.Add($username, $commonname);

$FullRead = [Microsoft.SharePoint.Administration.SPPolicyRoleType]::FullRead
$specialRole = $webApp.PolicyRoles.GetSpecialRole($FullRead)
$policy.PolicyRoleBindings.Add($specialRole);
$webApp.Update();

}

function addFullControl([String]$commonname, [String]$username, [String] $url){

$permissions = "FullControl"

$uri = New-Object System.Uri($url)
$webApp = [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup($uri)

$policy = $webApp.Policies.Add($username, $commonname);

$FullControl = [Microsoft.SharePoint.Administration.SPPolicyRoleType]::FullControl
$specialRole = $webApp.PolicyRoles.GetSpecialRole($FullControl)
$policy.PolicyRoleBindings.Add($specialRole);
$webApp.Update();
}


function addFullControlSystem([String]$commonname, [String]$username, [String] $url){

$permissions = "FullControl"

$uri = New-Object System.Uri($url)
$webApp = [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup($uri)

$policy = $webApp.Policies.Add($username, $commonname);

$FullControl = [Microsoft.SharePoint.Administration.SPPolicyRoleType]::FullControl
$specialRole = $webApp.PolicyRoles.GetSpecialRole($FullControl)
$policy.PolicyRoleBindings.Add($specialRole);
$policy.IsSystemUser = $true
$webApp.Update();
}

#bug #fix for multiple web applications
$webapps = get-spwebapplication |% { $_.Url }

	foreach ($webapp in $webapps){
		Write-Host -ForegroundColor Yellow "Permissions: Adding Web Application Permissions and Farm Level Permissions for $webapp."
		addFullControl "Farm Service Account" $env:serviceacct $webapp
		Write-Host -ForegroundColor Yellow "Permissions: Adding Web Application Permissions and Farm Level Permissions for $webapp. : COMPLETED"
	}
	
